/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AlumnosFrame.java
 *
 * Created on 09-ene-2012, 15:50:22
 */
package occho.view;

import java.sql.SQLException;
import java.util.List;
import java.util.Vector;
import occho.model.FachadaAdministrador;
import occho.model.Usuario;
import occho.view.resources.Messages;

/**
 *
 * @author inftel
 */
public class AlumnosFrame extends javax.swing.JInternalFrame implements Internacionalizable {
    
    private FachadaAdministrador fachada = null;
    private Usuario alumno = null;

    /** Creates new form AlumnosFrame
     * @param admin 
     */
    public AlumnosFrame(Usuario admin) {
        initComponents();
        
        setLenguaje();
        
        fachada = new FachadaAdministrador(admin);
        actualizarListaAlumnos();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        alumnosList = new javax.swing.JList();
        btnEliminar = new javax.swing.JButton();

        setClosable(true);
        setName("Form"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        alumnosList.setName("alumnosList"); // NOI18N
        alumnosList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                alumnosListValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(alumnosList);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getResourceMap(AlumnosFrame.class);
        btnEliminar.setText(resourceMap.getString("btnEliminar.text")); // NOI18N
        btnEliminar.setName("btnEliminar"); // NOI18N
        btnEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 374, Short.MAX_VALUE)
                    .addComponent(btnEliminar))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 227, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnEliminar)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnEliminarActionPerformed
        if (alumno != null) {
            try {
                if (fachada.eliminarUsuario(alumno)) {
                    alumno = null;
                    actualizarListaAlumnos();
                }
            } catch (ClassNotFoundException ex) {
                System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
            } catch (SQLException ex) {
                System.out.println("SQL_Exception: " + ex.getMessage());
            }
        }
    }//GEN-LAST:event_btnEliminarActionPerformed

    private void alumnosListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_alumnosListValueChanged
        if (alumnosList.getSelectedIndex() != -1) {
            alumno = (Usuario) alumnosList.getSelectedValue();
        } else {
            alumno = null;
        }
    }//GEN-LAST:event_alumnosListValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JList alumnosList;
    private javax.swing.JButton btnEliminar;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    private void actualizarListaAlumnos() {
        try {
            List<Usuario> alumnos = fachada.seleccionarAlumnos();
            alumnosList.setListData((Vector<Usuario>) alumnos);
        } catch (ClassNotFoundException ex) {
            System.out.println("Class_Not_Found_Exception: " + ex.getMessage());
        } catch (SQLException ex) {
            System.out.println("SQL_Exception: " + ex.getMessage());
        }
    }

    /**
     * Establece lenguaje
     */
    public void setLenguaje() {
        setTitle(Messages.getString("Form.title", Messages.BUNDLE_NAME_ALUMNOS));
        btnEliminar.setText(Messages.getString("btnEliminar.text", Messages.BUNDLE_NAME_ALUMNOS));
    }
}
